﻿//********************************************************************
// 
//  file:  frmEmails.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;

namespace Diomede.Explorer
{
    public partial class frmEmails : Form
    {
        public ISession Session { get; set; }

        public frmEmails()
        {
            InitializeComponent();
        }

        public frmEmails(ISession session)
        {
            InitializeComponent();
            this.Session = session;
        }

        private void frmEmails_Load(object sender, EventArgs e)
        {
            Timer tm = new Timer();
            tm.Interval = 500;
            tm.Tick += new EventHandler(tm_Tick);
            tm.Start();
        }

        void tm_Tick(object sender, EventArgs e)
        {
            (sender as Timer).Stop();
            loadEmails();
        }

        
        void loadEmails()
        {
            lbltext.Text = "Retrieving user emails";
            paneLoading.Visible = true;
            paneLoading.Refresh();

            IAccountManagement a = Session as IAccountManagement;
            IEmailsExecResult emailResult = a.GetEmailAddresses();
            if (emailResult.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                this.lv.Items.Clear();
                bool primary = false;
                foreach (var email in emailResult.Emails)
                {
                    ListViewItem item = this.lv.Items.Add(email);
                    if (!primary)
                    {
                        item.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
                        primary = true;
                    }
                }
                this.lv.Refresh();
            }
            paneLoading.Visible = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            InputBox frm = new InputBox("Add New Email", "Input Email Address", "", "");
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                IAccountManagement a = Session as IAccountManagement;
                ISessionExecResult result = a.AddEmail(frm.InputString);
                if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    MessageBox.Show(this, "Email successfully added.");
                    loadEmails();
                }
                else
                {
                    MessageBox.Show(this, result.Message);
                }
            }
        }

        private void lv_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (lv.SelectedIndices.Count > 1)
                {
                    deleteToolStripMenuItem.Enabled = true;
                    setAsPrimaryToolStripMenuItem.Enabled = false;
                }
                else if (lv.SelectedIndices.Count == 1)
                {
                    deleteToolStripMenuItem.Enabled = true;
                    setAsPrimaryToolStripMenuItem.Enabled = !lv.Items[0].Selected;
                }
                else
                {
                    deleteToolStripMenuItem.Enabled = false;
                    setAsPrimaryToolStripMenuItem.Enabled = false;
                }
                ctxMenu.Show(lv, e.Location);
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IAccountManagement a = Session as IAccountManagement;
            lbltext.Text = "Deleting email..";
            paneLoading.Visible = true;
            paneLoading.Refresh();
            
            foreach (ListViewItem item in lv.SelectedItems)
            {
                lbltext.Text = "Deleting " + item.Text;
                lbltext.Refresh();
                a.RemoveEmailAddress(item.Text);
            }
            paneLoading.Visible = false;
            loadEmails();
        }

        private void setAsPrimaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem item = lv.SelectedItems[0];
            lbltext.Text = string.Format("Setting {0} as new primary email.", item.Text);
            paneLoading.Visible = true;
            paneLoading.Refresh();
            
            IAccountManagement a = Session as IAccountManagement;
            a.SetPrimaryEmailAddress(item.Text);

            paneLoading.Visible = false;
            loadEmails();
        }

    }
}
